package com.ypx.ec.ware.service.impl;

import com.ypx.ec.common.constant.WareConstant;
import com.ypx.ec.ware.entity.PurchaseDetailEntity;
import com.ypx.ec.ware.service.PurchaseDetailService;
import com.ypx.ec.ware.vo.ItemVo;
import com.ypx.ec.ware.vo.MergeVo;
import com.ypx.ec.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ypx.ec.common.utils.PageUtils;
import com.ypx.ec.common.utils.Query;

import com.ypx.ec.ware.dao.PurchaseDao;
import com.ypx.ec.ware.entity.PurchaseEntity;
import com.ypx.ec.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if(purchaseId==null){
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }
        List<Long> items=mergeVo.getItems();
        Long finalPurchaseId=purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(item -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(item);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
    }

    @Override
    public PageUtils unreveive() {
        IPage<PurchaseEntity> page = this.page(new Query<PurchaseEntity>().getPage(new HashMap<>()),
                new QueryWrapper<PurchaseEntity>().notIn("status",WareConstant.PurchaseStatusEnum.RECEIVE)
        );
        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void received(List<Long> ids) {
        List<PurchaseEntity> collect = ids.stream().map(id -> {
            PurchaseEntity purchaseEntity = this.getById(id);
            return purchaseEntity;
        }).filter((ele) -> {
            if (ele.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() || ele.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
                return true;
            }
            return false;
        }).map(entity->{
            entity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            entity.setUpdateTime(new Date());
            return entity;
        }).collect(Collectors.toList());

        this.updateBatchById(collect);


        for (PurchaseEntity purchaseEntity : collect) {


            List<PurchaseDetailEntity> purchaseDetailEntities=
                    purchaseDetailService.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id",purchaseEntity.getId()));
            List<PurchaseDetailEntity> entities=purchaseDetailEntities.stream().map((pde)->{
                pde.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                return pde;
            }).collect(Collectors.toList());
            this.purchaseDetailService.updateBatchById(entities);

        }



    }
    @Transactional
    @Override
    public void done(PurchaseDoneVo purchaseDoneVo) {
        List<ItemVo> itemVos=purchaseDoneVo.getItems();
        boolean done=true;
        List<PurchaseDetailEntity> updateList=new ArrayList<>();
        for (ItemVo itemVo : itemVos) {
            PurchaseDetailEntity purchaseDetailEntity=new PurchaseDetailEntity();
            if(itemVo.getStatus()==WareConstant.PurchaseStatusEnum.HASERROR.getCode()){
                done=false;
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
            }else{
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());


            }
            purchaseDetailEntity.setId(itemVo.getItemId());
            updateList.add(purchaseDetailEntity);

        }
        purchaseDetailService.updateBatchById(updateList);

        if(done){
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setId(purchaseDoneVo.getId());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
            purchaseEntity.setUpdateTime(new Date());
            this.updateById(purchaseEntity);
        }



    }


}